﻿
var dataIF = [];
var ifEditId = -1;


$(document).ready(function () {
    initImpactFactors();

    $('a#impact-factor-modal-caller').fancybox({
        showCloseButton: false,
        fitToView: true,
        autoSize: true,
        closeClick: true,
        autoDimensions: false,
        width: 640,
        openEffect: 'none',
        padding: 0,
        onStart: function () {
            if (ifEditId == -1)
                $("#impact-factor-modal h1.header").text("Add Impact Factor 1");
            else
                $("#impact-factor-modal h1.header").text("Edit Impact Factor 1");
        },
        onClosed: function () {
            // Reset
            ifEditId = -1;
            $("span#vldEmission").hide();
        }
    });

    $("a#lnkAddIF, a#lnkAddIFBottom").click(function () {
        resetIFModal();
        $('a#impact-factor-modal-caller').click();

    });
});

function isGridPanelShown() {
    var len = $("#txtImpactFactors").length;

    return len > 0;
}

function callbackFromIFModal(row) {
    processIFData(row);
}

function getNextId(data) {
    var max = -1;
    for (var i = 0; i < data.length; i++) {
        var item = data[i];
        if (item.id > max)
            max = item.id;
    }

    return max + 1;
}

function processIFData(row) {
    var itemId = -1;

    if (ifEditId != -1) {
        for (var i = 0; i < dataIF.length; i++) {
            var item = dataIF[i];
            if (item.id == ifEditId) {
                item.item_id = row.item_id;
                item.item_name = row.item_name;
                item.emission = row.emission;
                item.ref_id = row.ref_id;
                item.ref_name = row.ref_name;

                itemId = item.id;
                break;
            }
        }
    }
    else {

        var inputRow = {
            id: getNextId(dataIF),
            item_id: row.item_id,
            item_name: row.item_name,
            emission: row.emission,
            ref_id: row.ref_id,
            ref_name: row.ref_name
        };

        itemId = inputRow.id;
        dataIF.push(inputRow);
    }

    updateIF();
    loadIFGrid();
    focusIFGrid(itemId);
}

function updateIF() {
    $("#txtImpactFactors").val(JSON.stringify(dataIF));

}

function focusIFGrid(id) {

    $("ui\\:scrollbox").scrollTop($('table#gridImpactFactors tr#' + id).offset().top);
    // $("ui\\:scrollbox").scrollTop($('table#gridImpactFactors tr:last').offset().top);
}

function initImpactFactors() {
    if (!isGridPanelShown())
        return;

    var str = $("#txtImpactFactors").val();
    if (str != '') {
        dataIF = JSON.parse(str);
        for (var i = 0; i < dataIF.length; i++) {
            var item = dataIF[i];
            item.id = i;
        }
    }


    loadIFGrid();
}

function deleteIFRow(id) {
    for (var i = 0; i < dataIF.length; i++) {
        var item = dataIF[i];
        if (item.id == id) {
            dataIF.splice(i, 1);
            break;
        }
    }
    updateIF();
    loadIFGrid();
    // focusIFGrid();
}

function editIFRow(id) {
    var selectedRow = null;
    for (var i = 0; i < dataIF.length; i++) {
        var item = dataIF[i];
        if (item.id == id) {
            selectedRow = item;
            break;
        }
    }

    ifEditId = id;

    resetIFModal();
    loadIFModal(selectedRow);
    $('a#impact-factor-modal-caller').click();
}

function loadIFGrid() {
    $('table#gridImpactFactors').empty();

    var oTable = $('table#gridImpactFactors').dataTable({
        "bProcessing": true,
        "bDestroy": true,
        "bPaginate": false,
        "bAutoWidth": false,
        "bFilter": false,
        "aaSorting": [],
        "aaData": dataIF,
        "aoColumns": [

                    { "mDataProp": "item_name", "bSortable": true, "sTitle": "Flow", "sClass": "cName", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "emission", "bSortable": false, "sTitle": "Emission", "sClass": "cEmission", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "ref_name", "bSortable": false, "sTitle": "Reference", "sClass": "cUnit", "bSearchable": true, "bVisible": true },
                    { "mDataProp": null, "bSortable": false, "sTitle": "", "sClass": "cDelete", "sDefaultContent": "<input id='btnEdit' class='button' type='button' value='Edit' /><input id='btnDelete' class='button' type='button' value='Delete' />", "bVisible": true }
                ],
        "fnCreatedRow": function (nRow, aData, iDataIndex) {
            // Assign id to 'tr'
            $(nRow).attr('id', aData.id);

            $('input#btnEdit', nRow).click(function (e) {
                editIFRow(aData.id);
            });

            $('input#btnDelete', nRow).click(function (e) {

                if (confirm('Are you sure to delete this?')) {
                    deleteIFRow(aData.id);
                }
            });
        }


    });

}